﻿namespace WMS6.Web.Configuration
{
    using System;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BaseInfoDetailServiceRef;

    public class BomDetail : BasePage
    {
        protected Button btnCancel;
        protected Button btnSave;
        protected HtmlForm F2123;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnType;
        protected Label lblAreaDetail;
        protected Label lblCompID;
        protected Label lblMsg;
        protected Label lblOwnerID;
        protected Label lblQty;
        protected Label lblSeqNo;
        protected Label lblSkuID;
        protected HtmlLink mycss;
        protected Label one1;
        protected ScriptManager ScriptManager1;
        protected TextBox txtCompID;
        protected TextBox txtOwnerID;
        protected TextBox txtQty;
        protected TextBox txtSeqNo;
        protected TextBox txtSkuID;

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            base.Response.Redirect(string.Format("Bom.aspx?from={0}&detail={1}&ID={2}&SkuID={3}", new object[] { base.UrlEncode(this.hdnFrom.Value), "1", base.UrlEncode(this.txtOwnerID.Text.Trim()), base.UrlEncode(this.txtSkuID.Text.Trim()) }));
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtCompID.Text))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F2270_006");
                this.txtCompID.Focus();
            }
            else if (string.IsNullOrEmpty(this.txtQty.Text))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F2270_007");
                this.txtQty.Focus();
            }
            else
            {
                bool success = false;
                decimal outDeci = 0.0M;
                try
                {
                    BomDetailInfo bomDetail = new BomDetailInfo {
                        OwnerID = this.txtOwnerID.Text.Trim(),
                        SkuID = this.txtSkuID.Text.Trim(),
                        ComponentSkuID = this.txtCompID.Text.Trim(),
                        SeqNo = this.txtSeqNo.Text.Trim()
                    };
                    if (decimal.TryParse(this.txtQty.Text.Trim(), out outDeci))
                    {
                        bomDetail.Qty = decimal.Parse(this.txtQty.Text.Trim());
                    }
                    else
                    {
                        bomDetail.Qty = outDeci;
                    }
                    bomDetail.CreatedBy = base.UserInfo.UserID;
                    bomDetail.UpdatedBy = bomDetail.CreatedBy;
                    if (!string.IsNullOrEmpty(this.hdnType.Value))
                    {
                        if (!string.IsNullOrEmpty(BaseInfoDetailServiceClientProxy.GetInstance().UpdateDetail(bomDetail, "BOM_DETAIL")))
                        {
                            success = true;
                        }
                    }
                    else if (!string.IsNullOrEmpty(BaseInfoDetailServiceClientProxy.GetInstance().InsertDetail(bomDetail, "BOM_DETAIL")))
                    {
                        success = true;
                    }
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F2270_008") + ex.Message;
                    return;
                }
                if (success)
                {
                    base.Response.Redirect(string.Format("Bom.aspx?from={0}&detail={1}&ID={2}&SkuID={3}", new object[] { base.UrlEncode(this.hdnFrom.Value), "1", base.UrlEncode(this.txtOwnerID.Text.Trim()), base.UrlEncode(this.txtSkuID.Text.Trim()) }));
                }
            }
        }

        private void LoadData()
        {
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"]))
            {
                string OwnerID = base.Request.QueryString["ID"];
                this.txtOwnerID.Text = OwnerID;
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["SkuID"]))
            {
                string SkuID = base.Request.QueryString["SkuID"];
                this.txtSkuID.Text = SkuID;
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["SeqNO"]))
            {
                string seqNo = base.Request.QueryString["SeqNO"];
                this.txtSeqNo.Text = seqNo;
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["ComponentSkuID"]))
            {
                string ComponentSkuID = base.Request.QueryString["ComponentSkuID"];
                this.txtCompID.Text = ComponentSkuID;
                this.txtCompID.Enabled = false;
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["QTY"]))
            {
                string QTY = base.Request.QueryString["QTY"];
                this.txtQty.Text = base.GetPreferenceDigit(QTY);
                this.hdnType.Value = "update";
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }
    }
}

